﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ViewCamera.PrivateImplementationDetails.CApVCUtil
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.ObjectModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ViewCamera.PrivateImplementationDetails;

public class CApVCUtil
{
  public static TxViewCamera GetSelectedViewCamera()
  {
    TxViewCamera selectedViewCamera = (TxViewCamera) null;
    if (TxApplication.ActiveSelection.Count == 1)
    {
      TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (TxViewCamera)));
      if (((Collection<ITxObject>) filteredItems).Count == 1)
        selectedViewCamera = ((Collection<ITxObject>) filteredItems)[0] as TxViewCamera;
    }
    return selectedViewCamera;
  }

  public static ITxOperation GetSelectedOperation()
  {
    ITxOperation selectedOperation = (ITxOperation) null;
    if (TxApplication.ActiveSelection.Count == 1)
    {
      TxObjectList filteredItems = TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) new TxTypeFilter(typeof (ITxOperation)));
      if (((Collection<ITxObject>) filteredItems).Count == 1)
        selectedOperation = ((Collection<ITxObject>) filteredItems)[0] as ITxOperation;
    }
    return selectedOperation;
  }

  public static TxObjectFlowOperation GetSelectedObjectFlowOperation()
  {
    TxObjectFlowOperation objectFlowOperation = (TxObjectFlowOperation) null;
    ITxOperation selectedOperation = CApVCUtil.GetSelectedOperation();
    if (selectedOperation != null)
    {
      objectFlowOperation = selectedOperation as TxObjectFlowOperation;
      if (TxObjectFlowOperation.op_Equality(objectFlowOperation, (ITxObject) null))
      {
        TxObjectFlowLocationOperation locationOperation = selectedOperation as TxObjectFlowLocationOperation;
        if (TxObjectFlowLocationOperation.op_Inequality(locationOperation, (ITxObject) null))
          objectFlowOperation = locationOperation.Collection as TxObjectFlowOperation;
      }
    }
    return objectFlowOperation;
  }

  public static TxObjectFlowLocationOperation GetRecentlyCreatedLocationOperation(
    TxObjectList prevLocs,
    TxObjectList newLocs)
  {
    foreach (ITxObject prevLoc in (Collection<ITxObject>) prevLocs)
      ((Collection<ITxObject>) newLocs).Remove(prevLoc);
    return ((Collection<ITxObject>) newLocs).Count > 0 ? ((Collection<ITxObject>) newLocs)[0] as TxObjectFlowLocationOperation : (TxObjectFlowLocationOperation) null;
  }

  public static TxGraphicViewer GetActiveGraphicViewer()
  {
    activeGraphicViewer = (TxGraphicViewer) null;
    switch (TxApplication.ViewersManager.GetActiveViewer())
    {
      case null:
      case TxGraphicViewer activeGraphicViewer:
        return activeGraphicViewer;
      default:
        activeGraphicViewer = TxApplication.ViewersManager.GraphicViewer;
        goto case null;
    }
  }

  public static bool IsActiveGraphicViewerCoupled()
  {
    TxGraphicViewer activeGraphicViewer = CApVCUtil.GetActiveGraphicViewer();
    return activeGraphicViewer != null && TxViewCamera.op_Inequality(activeGraphicViewer.GetCoupledCamera(), (ITxObject) null);
  }

  public static TxViewCamera GetCoupledCamera(TxGraphicViewer activeGV)
  {
    TxViewCamera coupledCamera = (TxViewCamera) null;
    if (activeGV != null)
      coupledCamera = activeGV.GetCoupledCamera();
    return coupledCamera;
  }

  public static bool IsGraphicViewerCoupled(TxGraphicViewer activeGV)
  {
    return activeGV != null && TxViewCamera.op_Inequality(activeGV.GetCoupledCamera(), (ITxObject) null);
  }

  public static void SetParallelViewMode()
  {
    if (TxGraphicViewerEx.The.ProjectionType == null)
      return;
    TxApplication.CommandsManager.ExecuteCommand("GraphicViewer.ParallelPerspective");
  }

  public static TxViewCamera CreateViewCamera(bool atPickLocation = false)
  {
    TxPhysicalRoot physicalRoot = TxApplication.ActiveDocument.PhysicalRoot;
    string uniqueDisplayName = physicalRoot.GetUniqueDisplayName(StringTable.NEW_VIEW_CAMERA_NAME_PREFIX);
    TxTransformation txTransformation = (TxTransformation) null;
    if (atPickLocation)
      txTransformation = TxApplication.ActiveSelection.GetLastPickedLocation();
    if (TxTransformation.op_Equality(txTransformation, (TxTransformation) null))
      txTransformation = new TxTransformation(new TxVector(-200.0, 0.0, 0.0), new TxVector(0.0, 0.0, 0.0), (TxTransformation.TxRotationType) 1);
    TxViewCameraCreationData cameraCreationData = new TxViewCameraCreationData();
    ((TxLocatableObjectCreationData) cameraCreationData).AbsoluteLocation = txTransformation;
    ((TxObjectCreationData) cameraCreationData).Name = uniqueDisplayName;
    return physicalRoot.CreateViewCamera(cameraCreationData);
  }

  public static void CoupleCameraToGraphicViewer(TxViewCamera camera, TxGraphicViewer activeGV)
  {
    if (activeGV == null)
      return;
    camera.CoupleToGraphicViewer(activeGV);
  }

  public static void CoupleCameraToGraphicViewer(TxViewCamera camera)
  {
    TxGraphicViewer activeGraphicViewer = CApVCUtil.GetActiveGraphicViewer();
    CApVCUtil.CoupleCameraToGraphicViewer(camera, activeGraphicViewer);
  }

  public static void CoupleCameraToGraphicViewer()
  {
    TxViewCamera selectedViewCamera = CApVCUtil.GetSelectedViewCamera();
    if (!TxViewCamera.op_Inequality(selectedViewCamera, (ITxObject) null))
      return;
    CApVCUtil.CoupleCameraToGraphicViewer(selectedViewCamera);
  }

  public static void DecoupleCameraFromGraphicViewer(TxGraphicViewer activeGV)
  {
    TxViewCamera coupledCamera = activeGV.GetCoupledCamera();
    if (!TxViewCamera.op_Inequality(coupledCamera, (ITxObject) null))
      return;
    coupledCamera.DecoupleFromGraphicViewer(activeGV);
  }

  public static void DecoupleCameraFromGraphicViewer()
  {
    TxGraphicViewer activeGraphicViewer = CApVCUtil.GetActiveGraphicViewer();
    if (activeGraphicViewer == null)
      return;
    CApVCUtil.DecoupleCameraFromGraphicViewer(activeGraphicViewer);
  }

  public static TxGraphicViewer OpenNewGraphicViewer()
  {
    TxGraphicViewer newGraphicViewer = TxApplication.ViewersManager.CreateNewGraphicViewer();
    TxViewersManagerEx.SetActiveViewer((TxViewerControl) newGraphicViewer);
    TxApplication.ViewersManager.BringGraphicViewerToFront((TxViewerControl) newGraphicViewer);
    newGraphicViewer.ZoomToFit();
    TxApplication.RefreshDisplay();
    return newGraphicViewer;
  }

  public static void OpenCameraViewer(bool displayError = true)
  {
    if (TxViewersManagerEx.GetAvailableGraphicViews() > 0)
    {
      TxGraphicViewer activeGV = CApVCUtil.OpenNewGraphicViewer();
      activeGV.DisplayOrientationFrameOfReference(false);
      TxViewCamera selectedViewCamera = CApVCUtil.GetSelectedViewCamera();
      if (!TxViewCamera.op_Inequality(selectedViewCamera, (ITxObject) null))
        return;
      CApVCUtil.CoupleCameraToGraphicViewer(selectedViewCamera, activeGV);
    }
    else
    {
      if (!displayError)
        return;
      int num = (int) TxMessageBox.Show(StringTable.OPEN_CV_ERROR_EXCEED_MAX_GRAPHIC_WINDOWS, StringTable.OPEN_CAMERA_VIEWER_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  public static void AddCurrentViewAsCameraLocation(bool displayError = true)
  {
    TxObjectFlowOperation objectFlowOperation = CApVCUtil.GetSelectedObjectFlowOperation();
    if (!TxObjectFlowOperation.op_Inequality(objectFlowOperation, (ITxObject) null))
      return;
    if (objectFlowOperation.SimulatedObject is TxViewCamera)
    {
      TxApplication.ActiveUndoManager.StartTransaction();
      TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (ITxLocationOperation));
      TxObjectList allDescendants1 = objectFlowOperation.GetAllDescendants((ITxTypeFilter) txTypeFilter);
      TxTransformation locationAlignedWith = TxViewCamera.GetLocationAlignedWith(CApVCUtil.GetActiveGraphicViewer(), true);
      bool flag = new TxCommandManager().ExecuteCommandByProgIdWithCmdParams("PathEditor.AppendLocationByResource.1", (object) null);
      if (flag)
      {
        TxObjectList allDescendants2 = objectFlowOperation.GetAllDescendants((ITxTypeFilter) txTypeFilter);
        flag = ((Collection<ITxObject>) allDescendants2).Count > ((Collection<ITxObject>) allDescendants1).Count;
        if (flag)
        {
          TxObjectFlowLocationOperation locationOperation = CApVCUtil.GetRecentlyCreatedLocationOperation(allDescendants1, allDescendants2);
          if (TxObjectFlowLocationOperation.op_Inequality(locationOperation, (ITxObject) null))
            locationOperation.AbsoluteLocation = locationAlignedWith;
        }
      }
      TxApplication.ActiveUndoManager.EndTransaction();
      TxApplication.RefreshDisplay();
      if (!flag)
        throw new SystemException();
    }
    else
    {
      if (!displayError)
        return;
      int num = (int) TxMessageBox.Show(StringTable.ADD_CV_LOCATION_ERROR_CAMERA_IS_NOT_SIMULATED_OBJECT, StringTable.ADD_CURRENT_VIEW_AS_CAMERA_LOCATION_NAME, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
  }

  public static void AlignCameraWithCurrentView()
  {
    TxViewCamera selectedViewCamera = CApVCUtil.GetSelectedViewCamera();
    if (!TxViewCamera.op_Inequality(selectedViewCamera, (ITxObject) null))
      return;
    TxGraphicViewer activeGraphicViewer = CApVCUtil.GetActiveGraphicViewer();
    selectedViewCamera.AlignWith(activeGraphicViewer, true);
    TxApplication.RefreshDisplay();
  }

  public static void AlignCameraViewer()
  {
    TxGraphicViewer activeGraphicViewer = CApVCUtil.GetActiveGraphicViewer();
    if (activeGraphicViewer == null)
      return;
    TxViewCamera coupledCamera = activeGraphicViewer.GetCoupledCamera();
    if (!TxViewCamera.op_Inequality(coupledCamera, (ITxObject) null))
      return;
    coupledCamera.AlignGraphicViewer(activeGraphicViewer);
  }

  public static bool OpenCameraViewer(TxViewCamera camera)
  {
    if (TxViewersManagerEx.GetAvailableGraphicViews() <= 0)
      return false;
    TxGraphicViewer activeGV = CApVCUtil.OpenNewGraphicViewer();
    CApVCUtil.CoupleCameraToGraphicViewer(camera, activeGV);
    return true;
  }
}
